package com.zzty.zzlp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.QuestionService;
import com.zzty.zzlp.service.TeamExamService;
import com.zzty.zzlp.service.TeamExamTransationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/teamgread")
public class TeamExamGreadController {
//    @Autowired
//    UserDao userDao;
    @Autowired
    QuestionService questionService;
    @Autowired
    TeamExamService teamExamService;
    @Autowired
    TeamExamTransationService teamExamTransationService;

    @RequestMapping(value = "/Call", produces = {"text/html;charset=UTF-8;"}, method = RequestMethod.POST)
    @ResponseBody
    public String scoreSearch(@RequestParam(value = "date") String date, HttpServletRequest request) throws Exception {
        System.out.println("go to search bu date");
        User user = (User) request.getSession().getAttribute("User");
//        System.out.println(date);
        Integer clazzId = user.getClazzId();
//		Integer clazzId =4;
        List<Integer> userids = new ArrayList<Integer>();

        Integer examId = teamExamService.searchExamId(date, clazzId);
        List<String> stuIds = teamExamTransationService.searchAllGroup(examId);
        List<String> stuNames = teamExamTransationService.searchUserIdsName(examId);

        String qstIds[] = teamExamService.getQuestionID(date, clazzId);
        String qstContent = questionService.Questionbyid(qstIds);
        if (qstContent.length() < 3) {
            qstContent = "当天无结对";
        }
        JSONArray jsonArray = new JSONArray();

        for (int i = 0; i < stuIds.size(); i++) {
            JSONObject json1 = new JSONObject();
            String stuids = stuIds.get(i);
            System.out.println(stuids);
            Integer totalTime = teamExamTransationService.searchTotalTimeById(examId, stuids);
            String answer = teamExamTransationService.searchAnswer(examId, stuids);
            String score = teamExamTransationService.searchScore(examId, stuids);
            String tTime = "" + totalTime;
            if (score == null) {
                score = "";
            }
            if (totalTime == null) {
                tTime = "";
            }
            Integer isValid = teamExamTransationService.searchValid(examId, stuids);
            stuids = stuids.replaceAll("\\|", "-");
            json1.put("stuIds", stuids);
            json1.put("stuNames", stuNames.get(i));
            json1.put("qstContent", qstContent);
            json1.put("totalTime", tTime);
            json1.put("answer", answer);
            json1.put("score", score);
            json1.put("isValid", isValid);
            jsonArray.add(json1);
        }
        return jsonArray.toString();
    }

}
